//
//  AudioPlayPageCollectionViewCell.m
//  QDaily_Program
//
//  Created by dllo on 15/12/10.
//  Copyright © 2015年 WXCLegendary. All rights reserved.
//

#import "AudioPlayPageCollectionViewCell.h"

@implementation AudioPlayPageCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_titleLabel];

        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_detailLabel];

        
        _audioImage = [[UIImageView alloc] init];
        [self addSubview:_audioImage];

        
        //为image添加播放手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        _audioImage.userInteractionEnabled = YES;
        [_audioImage addGestureRecognizer:tap];

        _timeImage = [[UIImageView alloc] init];
        _timeImage.image = [UIImage imageNamed:@"time"];
        [self addSubview:self.timeImage];

        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor grayColor];
        [self addSubview:self.timeLabel];

        
        //播放次数
        
        self.playImage = [[UIImageView alloc] init];
        self.playImage.image = [UIImage imageNamed:@"play"];
        [self addSubview:self.playImage];

        
        
        self.playCountLabel = [[UILabel alloc] init];
        self.playCountLabel.font = [UIFont systemFontOfSize:12];
        self.playCountLabel.textColor = [UIColor grayColor];
        [self addSubview:self.playCountLabel];
        
        
        
        //评论人数
        
        self.penImage = [[UIImageView alloc] init];
        self.penImage.image = [UIImage imageNamed:@"pen"];
        [self addSubview:self.penImage];

        
        self.commentCountLabel = [[UILabel alloc] init];
        self.commentCountLabel.font = [UIFont systemFontOfSize:12];
        self.commentCountLabel.textColor = [UIColor grayColor];
        [self addSubview:self.commentCountLabel];

        
        
        //分享button
        self.shareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self.shareButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
        [self addSubview:self.shareButton];

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLabel) name:@"nightMode" object:nil];
        [self changeLabel];
        
        self.startPlayImage = [[UIImageView alloc] init];
        self.startPlayImage.image = [UIImage imageNamed:@"play"];
        [self addSubview:self.startPlayImage];

    }
    return self;
}


- (void)changeLabel {
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSString *password = [user objectForKey:@"night"];
//    if ([password isEqualToString:@"yes"]) {
//        self.backgroundColor = [UIColor blackColor];
//        self.titleLabel.textColor = [UIColor whiteColor];
//        self.detailLabel.textColor = [UIColor whiteColor];
//        self.playCountLabel.textColor = [UIColor whiteColor];
//        self.commentCountLabel.textColor = [UIColor whiteColor];
//        self.timeLabel.textColor = [UIColor whiteColor];
//    }else {
//        self.backgroundColor = [UIColor whiteColor];
//        self.titleLabel.textColor = [UIColor blackColor];
//        self.detailLabel.textColor = [UIColor blackColor];
//        self.playCountLabel.textColor = [UIColor grayColor];
//        self.commentCountLabel.textColor = [UIColor grayColor];
//        self.timeLabel.textColor = [UIColor grayColor];
//    }
}



- (void)setAudioPlayerModel:(AudioPlayerModel *)audioPlayerModel {
    if (_audioPlayerModel != audioPlayerModel) {
        _audioPlayerModel = audioPlayerModel;
    }
    _titleLabel.text = _audioPlayerModel.title;
    _detailLabel.text = _audioPlayerModel.audioDescription;
    [_audioImage sd_setImageWithURL:[NSURL URLWithString:_audioPlayerModel.cover]];
    self.timeLabel.text = _audioPlayerModel.ptime;
    

    //播放次数
    float count = _audioPlayerModel.playCount.integerValue / 10000.0;
    
    self.playCountLabel.text = [NSString stringWithFormat:@"%.1f万", count];
    
    //评论次数
    self.commentCountLabel.text = [NSString stringWithFormat:@"%@", _audioPlayerModel.length];
    
    

    NSLog(@"%@", self.audioPlayerModel.mp4_url);
    
    
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    _titleLabel.frame = CGRectMake(5, 0, self.width - 10, 30);
    _detailLabel.frame = CGRectMake(5, _titleLabel.bottom, self.width - 10, 30);
    _audioImage.frame = CGRectMake(5, _detailLabel.bottom, self.width - 10, (self.width - 10) * 0.75);
    self.timeImage.frame = CGRectMake(5, self.audioImage.bottom + 20, 15, 15);
    [self.timeImage setCenterY:(self.height - self.audioImage.bottom) / 2 + self.audioImage.bottom];
    
    
    self.timeLabel.frame = CGRectMake(self.timeImage.right + 10, self.timeImage.top, 150, 15);
    
    //播放次数
    self.playImage.frame = CGRectMake(self.timeLabel.right + 10, self.timeImage.top, 15, 15);
    self.playCountLabel.frame = CGRectMake(self.playImage.right + 5, self.timeImage.top, 40, 15);
    
    //评论次数
    self.penImage.frame = CGRectMake(self.playCountLabel.right, self.timeImage.top, 15, 15);
    self.commentCountLabel.frame = CGRectMake(self.penImage.right + 5, self.timeImage.top, 30, 15);
    
    self.shareButton.frame = CGRectMake(self.commentCountLabel.right + 10, self.timeImage.top, 15, 15);
    
    
    [self.startPlayImage setSize:CGSizeMake(50, 50)];
    self.startPlayImage.center = self.audioImage.center;
    
    
}

- (void)tapAction {

    [self.delegate passAudioPlayerModel:self.audioPlayerModel];
    
}






@end
